package com.hrsaas.ext.spider.test.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.hrsaas.ext.spider.client.HeaderFactory;
import com.hrsaas.ext.spider.core.CookieStore;
import com.hrsaas.ext.spider.core.HttpClient;
import com.hrsaas.ext.spider.test.domain.Context;
import com.hrsaas.ext.spider.test.domain.LGJsonBean;
import com.hrsaas.ext.spider.test.domain.LGResumeBean;
import com.hrsaas.ext.spider.test.domain.LGResumeEnum;

import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponseStatus;

/**
 * <p>
 * 功能：拉去拉钩简历
 * <p>
 * 
 * @author wangxiaoliang@kanzhun.com
 *
 * 2018年8月7日
 */
public class LGCoreResumeHandler extends SessionMaintain {
	
	private HttpClient client;
	private CookieStore cookies;
	private HttpHeaders headers;

	public LGCoreResumeHandler(HttpClient client, CookieStore cookies, HttpHeaders headers) {
		this.client = client;
		this.cookies = cookies;
		this.headers = headers;
	}

	/**
	 * 简历拉取
	 */
	public void start() {
		//	新简历 / 待沟通简历 / 面试简历 / 录用简历
		for (LGResumeEnum identifiers : LGResumeEnum.values()) {
			// 简历列表数拉取
			resumelayout(identifiers.getName());
		}
	}

	/**
	 * 根据标识拉取简历列表
	 * @param name
	 */
	private void resumelayout(String identifier) {
		final HashMap<String,String> selectedArgs = Maps.newHashMap();
		selectedArgs.put("can", "true");
		selectedArgs.put("stage", identifier);
		selectedArgs.put("needQueryAmount", "true");
		// 递归函数
		resumelist(selectedArgs);
	}

	/**
	 * 递归函数，根据结果集进行分页拉取
	 * @param selectedArgs
	 */
	public void resumelist(final Map<String, String> selectedArgs) {
		client.post().setURL("https://easy.lagou.com/can/new/list.json")
			.addHeaders(headers)
			.addHeaders(HeaderFactory.LOGINJSON)
			.addReferer("https://easy.lagou.com/can/new/index.htm?can=true&stage=NEW&needQueryAmount=true")
			.addCookieStore(cookies)
			.addQueryPairs(selectedArgs)
			.execute(new LGResponseHandler<LGJsonBean>() {
				@Override
				protected boolean receive(HttpResponseStatus status, CookieStore cookieStore,
						HttpHeaders headers, LGJsonBean lson) {
					System.out.println(lson);
					if (HttpResponseStatus.OK.equals(status) && lson != null) {
						// 请求成功的响应码,接收分页数据响应
						if (lson.getState() == 1) {
							if (selectedArgs.get("pageNo") == null) {
								concurrentlist(selectedArgs, lson);
							}
							initResume(lson.getResumeUrls());
						} else if (lson.getState() == 299){ 		// 拉取频率被限制
							switchContent(selectedArgs);
						}
					}
					return false;
				}
				private void concurrentlist(final Map<String, String> selectedArgs, LGJsonBean bean) {
					Integer start = Integer.valueOf(selectedArgs.getOrDefault("pageNo", "1"));
					int total = bean.getTotalPageCount();
					while (total > start++) {
						HashMap<String, String> newArgs = Maps.newHashMap(selectedArgs);
						newArgs.put("subStage", "");
						newArgs.put("pageNo", start.toString());
						switchContent(newArgs);
//						resumelist(newArgs);
					}
				}
		});
		
	}
	
	/**
	 * 迭代url进行简历下载
	 * @param urls
	 */
	protected void initResume(List<String> resumes) {
		if (resumes == null || resumes.isEmpty()) return;
		for (String resume : resumes) {
//			switchContent(resume);
		}
	}
	
	/**
	 * 下载简历详情数据
	 * @param username
	 * @param client
	 * @param cookieStore
	 * @param list
	 */
	public void downloadResume(String url) {
		client.get()
			.setURL(url)
			.addHeaders(headers)
			.addHeaders(HeaderFactory.LOGINJSON)
			.addReferer("https://easy.lagou.com/can/new/index.htm?can=true&stage=NEW&needQueryAmount=true")
			.addCookieStore(this.cookies)
			.execute(new LGResponseHandler<LGResumeBean>() {
				@Override
				protected boolean receive(HttpResponseStatus status, CookieStore cookieStore,
						HttpHeaders headers, LGResumeBean rbean) {
					System.out.println(rbean);
					if (HttpResponseStatus.OK.equals(status) && rbean != null) {
						if (rbean.getState() == 1) {
							// 入库操作
						}  else if (rbean.getState() == 299){ 		// 拉取频率被限制
							switchContent(url);
						}
					}
					return false;
				}
		});
	}
	
	// 保存请求时的数据
	protected void switchContent(Map<String, String> selectedArgs) {
		Context context = new Context();
		context.setStep(Context.Step.LIST);
		context.setSelectedArgs(selectedArgs);
		context.setHandler(this);
		addContext(context);
	}	
	
	// 保存请求时的数据
	protected void switchContent(String url) {
		Context context = new Context();
		context.setStep(Context.Step.DOWNLOAD);
		context.setUrl(url);
		context.setHandler(this);
		addContext(context);
	}		
}
